﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class NewsEdit : System.Web.UI.Page
{
    #region METHODS

    protected void RefreshNewsList(string SelectValue)
    {
        ddlNews.Items.Clear();

        ddlNews.Items.Add(new ListItem(Resources.Language.NewNews, ""));
        ddlNews.DataSource = ReportsManager.Instance.GetNews(CommonTools.CurrentLanguageID);
        ddlNews.DataBind();

        ddlNews.SelectedValue = SelectValue;

        if (ddlNews.SelectedValue != string.Empty)
        {
            btnDeleteTop.Enabled = true;
            btnDeleteBottom.Enabled = true;
        }
    }

    protected void ClearNewsData()
    {
        heContent.Text = string.Empty;
        tbDescription.Text = string.Empty;
        tbTitle.Text = string.Empty;
        btnDeleteTop.Enabled = false;
        btnDeleteBottom.Enabled = false;
    }

    protected void RefreshNewsData()
    {
        ShopModel.News CurrentNews = ReportsManager.Instance.GetNews(int.Parse(ddlNews.SelectedValue), int.Parse(ddlLanguage.SelectedValue));

        if (CurrentNews != null)
        {
            heContent.Text = CurrentNews.Content;
            tbDescription.Text = CurrentNews.Description;
            tbTitle.Text = CurrentNews.Title;
            btnDeleteTop.Enabled = true;
            btnDeleteBottom.Enabled = true;
        }
    }

    #endregion METHODS

    #region EVENTS

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlNews.SelectedValue != string.Empty)
            RefreshNewsData();
    }

    protected void ddlNews_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlNews.SelectedValue != string.Empty)
            RefreshNewsData();
        else
            ClearNewsData();
    }

    protected void New_Click(object sender, EventArgs e)
    {
        ClearNewsData();
        ddlNews.SelectedIndex = 0;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        News news = new News(tbTitle.Text, tbDescription.Text, heContent.Text, DateTime.Now);

        if (ddlNews.SelectedValue != string.Empty)
            news.NewsID = int.Parse(ddlNews.SelectedValue);

        int id = ReportsManager.Instance.SaveNews(news, int.Parse(ddlLanguage.SelectedValue));

        if (id > 0)
            CtrlShowInfo.ShowInfo(false, Resources.Language.cmsMessageDataSaveSuccessful);
        else
            CtrlShowInfo.ShowInfo(true, Resources.Language.cmsMessageDataSaveFailure);

        RefreshNewsList(id.ToString());
    }

    protected void Delete_Click(object sender, EventArgs e)
    {
        if (ReportsManager.Instance.DeleteNews(int.Parse(ddlNews.SelectedValue)))
        {
            CtrlShowInfo.ShowInfo(false, Resources.Language.NewsDeleted);
        }
        else
        {
            CtrlShowInfo.ShowInfo(true, Resources.Language.Error_WhileDeleting);
        }
        RefreshNewsList(string.Empty);
        ClearNewsData();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RefreshNewsList(string.Empty);

            ddlLanguage.DataSource = ReportsManager.Instance.GetAvailableLanguages(true);
            ddlLanguage.DataBind();

            ddlLanguage.SelectedValue = CommonTools.CurrentLanguageID.ToString();
        }
    }

    #endregion PAGE EVENTS
}
